package com.nondo.rdp.finance.endpoint.api.admin.setting.home;

import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.setting.home.SettingModuleNotice;
import com.nondo.rdp.finance.logic.SettingModule.SettingModuleLogic;
import com.nondo.rdp.finance.model.setting.home.SettingModuleNoticeBean;
import com.nondo.rdp.finance.service.setting.home.SettingModuleNoticeService;
import com.nondo.rdp.finance.service.setting.home.SettingModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController("apiAdminSettingHomeModuleNotice")
@RequestMapping("/api/admin/setting/home/module/notice")
public class ModuleNoticeEndpoint {
    @Autowired
    SettingModuleService settingModuleService;
    @Autowired
    SettingModuleNoticeService settingModuleNoticeService;
    @Autowired
    SettingModuleLogic settingModuleLogic;

    @RequestMapping("/list")
    public View list() {
        View view = new View(20000, "获取列表成功");

        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleNotice> list = this.settingModuleNoticeService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModuleNotice item : list) {
            SettingModuleNoticeBean settingModuleNoticeBean = new SettingModuleNoticeBean();
            BeanUtils.copy(item, settingModuleNoticeBean);

            newList.add(settingModuleNoticeBean);
        }

        view.setData(newList);

        return view;
    }

    @RequestMapping("/add")
    public View add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody SettingModuleNotice settingModuleNotice, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //判断提交的更多类型是不是有效的
            boolean checkTypeValue = settingModuleLogic.checkTypeValue(settingModuleNotice.getType(), settingModuleNotice.getValue());
            if (!checkTypeValue) {
                return View.ofError("操作类型和值不匹配");
            }
            this.settingModuleNoticeService.save(settingModuleNotice);
        }

        return view;
    }

    @RequestMapping("/update")
    public View update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody SettingModuleNoticeBean settingModuleNoticeBean, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //判断提交的更多类型是不是有效的
            boolean checkTypeValue = settingModuleLogic.checkTypeValue(settingModuleNoticeBean.getType(), settingModuleNoticeBean.getValue());
            if (!checkTypeValue) {
                return View.ofError("操作类型和值不匹配");
            }

            //数据拷贝
            SettingModuleNotice settingModuleNotice = new SettingModuleNotice();
            BeanUtils.copy(settingModuleNoticeBean, settingModuleNotice);

            this.settingModuleNoticeService.update(settingModuleNotice);
        }

        return view;
    }

    @RequestMapping("/delete/{id}")
    public View delete(@PathVariable("id") Long id) {
        this.settingModuleNoticeService.delete(id);

        return View.ofOk("删除成功");
    }
}